package com.boco.pciv.core.controller;

import com.boco.pciv.core.exception.AuthFailException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by CC on 2017/7/5.
 * 统一异常处理
 */
@ControllerAdvice(assignableTypes  =  {BaseController.class})
public class BaseMvcExceptionHandler {
    //logger
    private static final Logger logger = LoggerFactory.getLogger(BaseMvcExceptionHandler.class);

    @ExceptionHandler({ Exception.class })
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ModelAndView defaultExpcetionHandler(HttpServletRequest req, Exception  e) {
        logger.warn("系统异常",e);
        ModelAndView m = new ModelAndView();
        m.addObject("url", req.getRequestURL());
        m.addObject("exception", e.getMessage());
        m.setViewName("error/500");
        return m;
    }

    @ExceptionHandler({AuthFailException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ModelAndView authExpcetionHandler(AuthFailException e) {
        ModelAndView m = new ModelAndView();
        m.setViewName("login");
        return m;
    }
}
